package com.block.product.service.union.utils.ssd;

import cn.hutool.core.codec.Base64Decoder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;


/**
 * 接口加解密流程
 */
public class RequestUtil51GJJ {
	// 这里是两套密钥，demo加解密方便测试用的是同一套

	// TODO 此处值为示例，具体值由51公积金提供
	private final static String PUCLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqlfpBtY0eYAIDIa9CL3fgz5aD76PqRyNCCnwXHG9ItTzHcSxJvMjSqCynQCeqX+psqSbXq8vuEB9/rorbv/5mVUw4TlwWOUIU36ALKuM8P9L+8N2a0fsyVstsPXRimrxmu2UQrJ309a63vQCCAVyW/QyHP54prr3h8Hcfarc66ElVxpMRXSjMvpitsxUdw2prAL0eqw/fZvOQ1yoaw4/z0EbKvEG7SgJBn85pvrSmq/cz9tNfa00S9YaZH2JhXzvFC2mY+B35jdR9ExH1jpNiYPyCpmxrC4Djnbm0F7AH1GYl9NDePd7BOIjQsJ/HDO22kOuew021tMGPcmeUnKRhQIDAQAB";
	// TODO 此处值为贵方自己的私钥
	private final static String PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCqV+kG1jR5gAgMhr0Ivd+DPloPvo+pHI0IKfBccb0i1PMdxLEm8yNKoLKdAJ6pf6mypJtery+4QH3+uitu//mZVTDhOXBY5QhTfoAsq4zw/0v7w3ZrR+zJWy2w9dGKavGa7ZRCsnfT1rre9AIIBXJb9DIc/nimuveHwdx9qtzroSVXGkxFdKMy+mK2zFR3DamsAvR6rD99m85DXKhrDj/PQRsq8QbtKAkGfzmm+tKar9zP2019rTRL1hpkfYmFfO8ULaZj4HfmN1H0TEfWOk2Jg/IKmbGsLgOOdubQXsAfUZiX00N493sE4iNCwn8cM7baQ657DTbW0wY9yZ5ScpGFAgMBAAECggEAAjjpYlxzDM69zCQJ3U5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vpV9ulMJBwTLxSoeWY3N/fzT8XZkiL45GG866etpUSj/su5fSkxpcXms/avIZ6o91fRH2iwVCNyKtlwaYZS04QsSbj91yPRhFy8B7Lnli3ZIUEH/Xw==";

	/**
	 * 组装http请求的入参
	 * 
	 * @return
	 */
	public static JSONObject encodeParam(JSONObject paramJson, String pubKey, String priKey) {
		// http请求入参组装
		JSONObject data = new JSONObject();
		// 业务参数json字符串组装
		String source = paramJson.toString();
		try {
			// 加密部分demo
			// 从字符串中得到公钥
			PublicKey publicKey = RSAUtils.loadPublicKey(pubKey);

			System.out.println("需加密的明文：" + source);
			byte[] encryptByte = RSAUtils.encryptData(source.getBytes("utf-8"), publicKey);
			// 为了方便观察把加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
			String afterencrypt = Base64Utils.encode(encryptByte);
			System.out.println("加密后的密文：" + afterencrypt);// java.net.URLEncoder.encode(afterencrypt,"utf-8"));
			data.put("content", afterencrypt);
			// 从字符串中得到私钥
			PrivateKey privateKey = RSAUtils.loadPrivateKey(priKey);
			// 加签
			String signStr = RSAUtils.rsaSign(encryptByte, privateKey);
			System.out.println("加签后的签名：" + signStr);
			data.put("sign", signStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("加密后的json：" + data);
		return data;
	}

	/**
	 * 解密、验签，获取接口业务入参
	 * 
	 * @param data http请求入参
	 * @return
	 */
	public static JSONObject decodeParam(JSONObject data, String pubKey, String priKey) {
		String afterencrypt = data.getString("content");
		System.out.println("需解密的密文：" + afterencrypt);// java.net.URLEncoder.encode(afterencrypt,"utf-8"));
		String signStr = data.getString("sign");
		JSONObject paramJson = new JSONObject();
		try {
			// 从字符串中得到公钥
			PublicKey publicKey = RSAUtils.loadPublicKey(pubKey);
			// 从字符串中得到私钥
			PrivateKey privateKey = RSAUtils.loadPrivateKey(priKey);
			// 因为RSA加密后的内容经Base64再加密转换了一下，所以先Base64解密回来再给RSA解密
			byte[] decryptByte = RSAUtils.decryptData(Base64Decoder.decode(afterencrypt), privateKey);
			String decryptStr = new String(decryptByte);
			System.out.println("解密后的明文：" + decryptStr);
			paramJson = JSON.parseObject(decryptStr);
			// 验签
			boolean result = RSAUtils.doCheck(Base64Decoder.decode(afterencrypt), Base64Utils.decode(signStr), publicKey);
			System.out.println("验签的结果：" + result);
			if (!result) {
				System.out.println("验签失败");
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramJson;
	}

//	public static Map<String, String> buildParam(JSONObject param) {
//		JSONObject requestParam = encodeParam(param, PUCLIC_KEY, PRIVATE_KEY);
//		System.out.println("请求json：" + requestParam);
//
//		JSONObject responseJson = JSON.parseObject(requestParam);
//		System.out.println("响应json：" + responseJson);
//		JSONObject responseParam = decodeParam(responseJson);
//		return responseParam;
//	}

//	public static JSONObject getResponse(JSONObject param) {
//		return encodeParam(param);
//	}

}
